<?php
require_once("db_functions.php");
require_once("page.class.php");

define("PAGE_LEN",15);

class delatnost extends page{
	var $db;
	
	function delatnost($get,$post){
		$this->db = new db_functions();
		
		$action = isset($get["a"])?$get["a"]:$post["a"];
		$mod = getFromInput("mod");
		switch ($action){
			case "saveDelatnost":
				$name = readValFromInput("name");
				$name_eng = readValFromInput("name_eng");
				
				$q = "UPDATE delatnosti SET delatnost_ime='$name', delatnost_ime_eng='$name_eng' WHERE delatnost_id=".$post["id"];
				if(!$this->db->alterTable($q)){
					echo mysql_error();
				}else{
					$q = "UPDATE delatnosti_kategorije SET delkat_katid=".$post["kat"]." WHERE delkat_delid=".$post["id"];
					if($this->db->alterTable($q)){
						echo "Delatnost je izmenjena.";
					}else{
						echo mysql_error();
					}
				}
				break;
				
			case "deleteDelatnost":
				$q = "DELETE FROM delatnosti WHERE delatnost_id=".$get["id"];
				if(!$this->db->alterTable($q)){
					echo mysql_error();
				}else{
					$q = "DELETE FROM delatnosti_kategorije WHERE delkat_delid=".$get["id"];
					if($this->db->alterTable($q)){
						echo "Delatnost je obrisana.";
					}else{
						echo mysql_error();
					}
				}
				break;
				
			case "addDelatnost":
				$kat = $get["k"];
				$name = readValFromInput("name");
				$name_eng = readValFromInput("name_eng");
				
				if(!$this->db->getOne("SELECT 1 FROM delatnosti WHERE delatnost_ime='$name'")){
					$q = "INSERT INTO delatnosti(delatnost_ime,delatnost_ime_eng) VALUES('$name','$name_eng')";
					
					if(!$this->db->alterTable($q)){
						echo mysql_error();
					}else{
						$id = $this->db->getOne("SELECT LAST_INSERT_ID() FROM delatnosti");
						$q = "INSERT INTO delatnosti_kategorije VALUES('$id','$kat')";
						if($this->db->alterTable($q)){
							echo "nova delatnost je dodata.";
						}else{
							echo mysql_error();
						}
					}
				}else{
					echo "Delatnost $name već postoji.";
				}
				
				break;
			case "kategorije":
				$this->browseKategorije(getFromInput("p"),getFromInput("k"));
				break;
			case "addKategorija":
				$name = trim(getFromInput("k"));
				$name_eng = trim(getFromInput("ke"));
				
				$q = "INSERT INTO kategorije VALUES('','$name','$name_eng')";
				if($this->db->alterTable($q)){
					echo "Kategorija je unešena";
				}else{
					echo mysql_error();
				}
				
				break;
			case "saveKategorija":
				$id = getFromInput("id");
				$name = readValFromInput("k");
				$name_eng = readValFromInput("ke");
				
				$q = "UPDATE kategorije SET kat_ime='$name', kat_ime_eng='$name_eng' WHERE kat_id=$id";
				if($this->db->alterTable($q)){
					echo "Izmene sačuvane.";
				}else{
					echo mysql_error();
				}
				break;
			case "deleteKategorija":
				$q = "DELETE FROM kategorije WHERE kat_id=".$get["id"];
				if($this->db->alterTable($q)){
					echo "Kategorija je obrisana.";
				}else{
					echo mysql_error();
				}
				break;
			case "inherit":
				switch ($mod){
					case "save":
						$child = getFromInput("child");
						$parent = getFromInput("parent");
						$q = "INSERT INTO delatnosti_nasledjivanje(child,parent) VALUES($child,$parent)";
						if ($this->db->alterTable($q)) {
							$msg = "Izmene sačuvane.";
						}else{
							$msg = mysql_error();
						}
						echo "$msg{!}$parent";
						break;
					case "delete":
						$id = getFromInput("id");
						$parent=getFromInput("parent");
						$q = "DELETE from delatnosti_nasledjivanje WHERE id=$id LIMIT 1";
						if ($this->db->alterTable($q)) {
							$msg = "Delatnost uspešno obrisana.";
						}else{
							$msg = mysql_error();
						}
						echo "$msg{!}$parent";
						break;
					default:
						$this->delsInheritance();
						break;
				}
				break;
			case "family":
				switch ($mod){
					case "new":
						$this->delsFamilyNew();
						break;
					case "add":
						$this->delsFamilyAdd();
						break;
					case "delete":
						$this->delsFamilyDelete();
						break;
					case "empty":
						$this->delsFamilyEmpty();
						break;
					default:
						$this->delsFamily();
						break;
				}
				break;
			default:
				$filter = array();
				$filter["d"] = getFromInput("d");
				$filter["k"] = getFromInput("k");
				
				$this->browseDelatnosti($post["p"],$filter);
				break;
		}
	}
	
	function browseDelatnosti($page,$filter){
		if(!isset($page)){
			$page = 1;
		}
		$start = PAGE_LEN*($page-1);
		
		$this->assign("filter_delatnost",$filter["d"]);
		$this->assign("filter_kategorija",$filter["k"]);
		
		if($filter["k"]>0){
			$polja = $this->get_Kategorije($filter["k"]);
		}else{
			$polja = $this->get_Kategorije();
		}
		/*foreach ($polja as $k=>$v){
			$polja[$k] = array_splice($polja[$k],$start,PAGE_LEN);
		}*/
		$this->assign("katid",$polja["polje_id"]);
		$this->assign("kat",$polja["polje_ime"]);
		$this->assign("kat_eng",$polja["polje_ime_eng"]);
		$this->assign("selected_kat",$polja["polje_selected"]);
		
		if($filter["k"]>0){
			if(strlen(trim($filter["d"]))){
				$polja = $this->get_Delatnosti($filter["k"],trim($filter["d"]));
			}else{
				$polja = $this->get_Delatnosti($filter["k"]);
			}
		}else{
			if($filter["d"]){
				$polja = $this->get_Delatnosti(null,trim($filter["d"]));
			}else{
				$polja = $this->get_Delatnosti();
			}
		}
		if($polja !== false){
			$ukupno = count($polja["polje_id"]);
			$this->assign("count",$ukupno);
			if($ukupno>PAGE_LEN){
				
				$num_pages = bcdiv($ukupno,PAGE_LEN,0);
				if($num_pages%PAGE_LEN>0){
					$num_pages++;
				}
				
				for($i=0; $i<$num_pages; $i++){
					$p[] = $i+1;
					if($i == ($page-1)){
						$cp[] = "current";
					}else{
						$cp[] = "";
					}
				}
				$this->assign("current_page",$cp);
				$this->assign("page",$p);
			}
			
			foreach ($polja as $k=>$v){
				$polja[$k] = array_splice($polja[$k],$start,PAGE_LEN);
			}
			$this->assign("delid",$polja["polje_id"]);
			$this->assign("del",$polja["polje_ime"]);
			$this->assign("del_eng",$polja["polje_ime_eng"]);
			$this->assign("del_katid",$polja["polje_kat"]);
			$this->assign("kat_name",$polja["polje_kat_name"]);
			$this->assign("selected_del",$polja["polje_selected"]);
		}else{
			$this->assign("count","0 ");
			$this->assign("start_comment","<!--");
			$this->assign("end_comment","-->");
		}
		
		echo $this->parseTemplate("delatnosti/delatnost.tpl");
	}
	function delsInheritance(){
		
		$parent = getFromInput("parent");
		
		$polja = $this->get_Delatnosti(null,$parent);
			$this->assign("del_id",$polja["polje_id"]);
			$this->assign("del_name",$polja["polje_ime"]);
			$this->assign("del_selected",$polja["polje_selected"]);
		
		if ($parent>0) {
			$q = "SELECT dn.*, d.delatnost_ime "
				."FROM delatnosti_nasledjivanje dn, delatnosti d "
				."WHERE parent=$parent "
				."AND dn.child=d.delatnost_id "
				."ORDER BY d.delatnost_ime";
			$res = $this->db->select2($q);
			if ($res !== false) {
				foreach ($res as $row){
					$ids[] = $row["id"];
					$parent_ids[] = $row["parent"];
					$child_ids[] = $row["child"];
					$child_names[] = $row["delatnost_ime"];
				}
				$this->assign("inherit_id",$ids);
				$this->assign("inherit_parent",$parent_ids);
				$this->assign("inherit_child",$child_ids);
				$this->assign("inherit_child_name",$child_names);
			}else{
				$this->assign("comment_start","<!--");
				$this->assign("comment_end","-->Nema delatnosti koje nasleđuju odabranu delatnost");
			}
		}
		echo $this->parseTemplate("delatnosti/delatnosti_nasledjivanje.tpl");
	}
	
	function delsFamily(){
		$display_new_family = "none";
		$display_add_del = "none";
		$display_delete_all = "none";
		
		$del = getFromInput("filter_del");
		
		$polja = $this->get_DelatnostiAll($del);
			$this->assign("delatnost_id", $polja["polje_id"]);
			$this->assign("delatnost_name", $polja["polje_ime"]);
			$this->assign("delatnost_selected", $polja["polje_selected"]);
		
		
		if ($del>0) {
			$family = $this->get_DelatnostiFamilies($del);
			
			$this->assign("family_group_name",$this->db->getOne("SELECT delatnost_ime FROM delatnosti WHERE delatnost_id=$del LIMIT 1"));
			
			$display_new_family = "block";
			$this->assign("family_group",$del);
			if ($family) {
				$display_add_del= "block";
				$display_new_family = "none";
				$display_delete_all = "block";
				$this->assign("family_del_id",$family["polje_id"]);
				$this->assign("family_del_ime",$family["polje_ime"]);
				$this->assign("family_del_group",$family["polje_group"]);
			}else{
				$this->assign("comment_start","<!--");
				$this->assign("comment_end","-->Nema srodnih delatnosti");
			}
		}
		
		$this->assign("display_new_family",$display_new_family);
		$this->assign("display_add_del",$display_add_del);
		$this->assign("display_delete_all",$display_delete_all);
		echo $this->parseTemplate("delatnosti/srodne_delatnosti.tpl");
	}	
	function delsFamilyNew(){
		$code = 0;
		$msg = "Familija delatnosti kreirana";
		
		$delid = getFromInput("delid");
		$delid2 = getFromInput("delid2");
		
		if(!$this->db->alterTable("INSERT INTO delatnosti_srodne VALUES($delid,$delid2)")){
			$code = 1;
			$msg = mysql_error();
		}
		
		echo "$code{!}$msg";
		
	}
	function delsFamilyAdd(){
		
		$code = 0;
		$msg = "Dealtnost dodata.";
		
		$delid = getFromInput("delid");
		$group = getFromInput("group");
		
		if($delid==$group){
			$code = 1;
			$msg = "Ne možete dodati delatnost u samu sebe!";
		}else if ($this->delsFamilyInFamily($delid,$group)) {
			$code = 1;
			$msg = "Delatnost već pripada odabranoj familiji delatnosti!";
		}
		/*else if ($this->delsFamilyInFamily($delid)) {
			$code = 1;
			$msg = "Delatnost već pripada nekoj drugoj familiji delatnosti!";
		}*/
		else{
			$q = "INSERT INTO delatnosti_srodne VALUES($group,$delid)";
			if (!$this->db->alterTable($q)) {
				$code = 1;
				$msg = mysql_error();
			}
		}
		
		echo "$code{!}$msg";
	}
	function delsFamilyDelete(){
		$code = 0;
		$msg = "Delatnost obrisana iz familije delatnosti.";
		
		$delid = getFromInput("delid");
		$group = getFromInput("group");
		
		if ($this->db->alterTable("DELETE FROM delatnosti_srodne WHERE id=$group AND delatnost=$delid LIMIT 1")) {
			//$ndel = $this->db->getOne("SELECT id FROM delatnosti_srodne WHERE id=$group LIMIT 1");
			$ndel = $group;
		}else{
			$code = 1;
			$msg = mysql_error();
		}
		
		echo "$code{!}$msg{!}$ndel";
	}
	function delsFamilyEmpty(){
		$code = 0;
		$msg = "Familija delatnosti obrisana";
		$group = getFromInput("group");
		
		$q = "DELETE FROM delatnosti_srodne WHERE id=$group";
		if (!$this->db->alterTable($q)) {
			$code = 1;
			$msg = mysql_error()."\n$q";
		}
		echo "$code{!}$msg";
		
	}
	function delsFamilyInFamily($delid,$id=null){
		$q = "SELECT 1 FROM delatnosti_srodne WHERE delatnost=$delid ";
		if ($id !== null) {
			$q .= "AND id=$id ";
		}
		$q .= "LIMIT 1";
		$ret = $this->db->getOne($q);
		return $ret>0?true:false;
	}
	
	function browseKategorije($page=null,$kat=null){
		if($kat){
			$kat = trim($kat);
		}
		
		$this->assign("filter_kategorija",$kat);
		if(!$page){
			$page = 1;
		}
		
		$this->assign("current_page_num",$page);
		
		$q = "SELECT * FROM kategorije ";
		if(strlen($kat)){
			$q .= "WHERE kat_ime LIKE '%$kat%' OR kat_ime_eng LIKE '%$kat%' ";
		}
		$q .= "ORDER BY kat_ime, kat_ime_eng";
		$resall = $this->db->select1($q);
		$this->assign("count",count($resall));
		
		$num_all = count($resall);
		
		if($num_all>PAGE_LEN){
			
			$num_pages = $num_all/PAGE_LEN;
			if($num_all%PAGE_LEN){
				$num_pages++;
			}
			for($i=1; $i<=$num_pages; $i++){
				$p[] = $i;
				if($i==$page){
					$curr[] = "current";
				}else{
					$curr[] = "";
				}
			}
			$this->assign("page",$p);
			$this->assign("current_page",$curr);
			
			$start = ($page-1)*PAGE_LEN;
			$end = $start+PAGE_LEN;
			
			$q .= " LIMIT $start,$end";
		}
		
		
		$res = $this->db->select2($q);
		
		if($res !== false){
			foreach ($res as $row){
				$ids[] = $row["kat_id"];
				$names[] = $row["kat_ime"];
				$names_eng[] = $row["kat_ime_eng"];
			}
			$this->assign("katid",$ids);
			$this->assign("kat",$names);
			$this->assign("kat_eng",$names_eng);
		}else{
			$this->assign("start_comment","<!--");
			$this->assign("end_comment","-->");
			$this->assign("count","0");
		}
		
		echo $this->parseTemplate("delatnosti/kategorija.tpl");
	}
	
}
?>